package com.szit.test.dao.impl;

import com.szit.test.dao.MySqlConnection;
import com.szit.test.dao.UserDao;
import com.szit.test.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 功能：用户数据访问层接口实现类
 *
 * @author 勇哥
 * @version 1.0 2025-05-25
 */
public class UserDaoImpl implements UserDao {
    private MySqlConnection db;

    public UserDaoImpl() {
        db = new MySqlConnection();
    }

    @Override
    public User getUserByUsername(String username) {
        String sql = "select * from users where username=?";
        Object[]parameters = {username};
        ResultSet rs = db.getResultSet(sql,parameters);
        User user = null;
        try{
            if(rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        db.close();
        return user;
    }
}
